
export default async function onekeylogin() {
    try {
        // 获取支持的 OAuth 服务提供商
        const providerRes = await new Promise((resolve, reject) => {
            uni.getProvider({
                service: 'oauth',
                success: (res) => resolve(res),
                fail: (err) => reject(err)
            });
        });

        // 调用一键登录
        const loginRes = await new Promise((resolve, reject) => {
            uni.login({
                provider: 'univerify',
                univerifyStyle: {
                    "fullScreen": true,
                    "backgroundColor": "#ffffff",
                    "backgroundImage": "",
                    "icon": {
                        "path": "static/pages/icon/013_talk.png",
                        "width": "60px",
                        "height": "60px"
                    },
                    "closeIcon": {
                        "path": "static/pages/icon/013_jiandao.png",
                        "width": "60px",
                        "height": "60px"
                    },
                    "phoneNum": {
                        "color": "#202020"
                    },
                    "slogan": {
                        "color": "#BBBBBB"
                    },
                    "authButton": {
                        "normalColor": "#9B1CFF",
                        "highlightColor": "#7530FF",
                        "disabledColor": "#73aaf5",
                        "textColor": "#ffffff",
                        "title": "本机号码一键登录",
                        "borderRadius": "10px"
                    },
                    "otherLoginButton": {
                        "visible": false,
                        "normalColor": "",
                        "highlightColor": "",
                        "textColor": "#656565",
                        "title": "其他登录方式",
                        "borderColor": "",
                        "borderRadius": "0px"
                    },
                    "privacyTerms": {
                        "defaultCheckBoxState": true,
                        "isCenterHint": false,
                        "uncheckedImage": "",
                        "checkedImage": "",
                        "checkBoxSize": 12,
                        "textColor": "#BBBBBB",
                        "termsColor": "#5496E3",
                        "prefix": "我已阅读并同意",
                        "suffix": "并使用本机号码登录",
                        "privacyItems": [
                            {
                                "url": "https://",
                                "title": "用户服务协议"
                            }
                        ]
                    },
                    "buttons": {
                        "iconWidth": "45px",
                        "list": [
                            {
                                "provider": "apple",
                                "iconPath": "/static/pages/icon/013_taxi.png"
                            },
                            {
                                "provider": "weixin",
                                "iconPath": "/static/pages/icon/013_taxi.png"
                            }
                        ]
                    }
                },
                success: (res) => resolve(res),
                fail: (err) => reject(err)
            });
        });

        const { openid, access_token } = loginRes.authResult;
        console.log(loginRes.authResult);

        // 调用云函数换取手机号
        const cloudFunctionRes = await uniCloud.callFunction({
            name: "oneKeyLogin",
            data: {
                openid,
                access_token
            }
        });

        if (!cloudFunctionRes.result.state) {
            uni.showToast({
                title: "登录失败",
                complete: () => {
                    uni.reLaunch({
                        url: ""
                    });
                }
            });
            return;
        }

        const phoneNumber = cloudFunctionRes.result.result.phoneNumber;
        console.log(phoneNumber);

        try {
            uni.setStorageSync("phoneNumber", phoneNumber);
        } catch (e) {
            console.log("存电话号失败！！");
        }

 
    } catch (error) {
        console.log(error.errCode);
        if (error.errCode === 30002) {
            uni.closeAuthView();
        }
        console.log(error.errMsg);
 
    }
}